<template>
  <div class="app-container">
    <el-card>
      <el-form size="small" :model="searchForm" inline>
        <el-form-item label="行业编码">
          <el-input v-model="searchForm.code" placeholder="请输入行业编码" />
        </el-form-item>
        <el-form-item label="行业名称">
          <el-input v-model="searchForm.name" placeholder="请输入行业名称" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          <el-button icon="el-icon-search" type="primary" @click="getList"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="addIndustry()"
        >新增行业</el-button
      >
      <el-table
        :data="tableData.data"
        style="width: 100%; margin-top: 20px"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        row-key="code"
        v-loading="listLoading"
      >
        <el-table-column prop="id" label="ID" align="center"></el-table-column>
        <el-table-column
          prop="code"
          label="行业编码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="行业名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="note"
          label="备注"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editIndustry(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="addIndustry(scope.row)"
              >新增下级</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="deleteIndustry(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      :title="detailForm.id ? '编辑行业' : '新增行业'"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="800px"
    >
      <el-form
        ref="detailForm"
        :model="detailForm"
        label-width="100px"
        size="small"
        :rules="rules"
      >
        <el-form-item
          label="上级行业"
          prop="parentName"
          v-if="detailForm.parentCode !== '0'"
        >
          <el-input disabled v-model="detailForm.parentName" placeholder="" />
        </el-form-item>
        <el-form-item label="行业编码" prop="code">
          <el-input v-model="detailForm.code" maxlength="4" placeholder="" />
        </el-form-item>
        <el-form-item label="行业名称" prop="name">
          <el-input v-model="detailForm.name" placeholder="" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="detailForm.note" placeholder="" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose()">取消</el-button>
          <el-button type="primary" @click="onSubmit('detailForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { handleTree } from "@/utils/auth";
import {
  industryList,
  industryAdd,
  industryEdit,
  industryDelete
} from "@/api/industry";
export default {
  name: "index",
  data() {
    return {
      listLoading: false,
      tableData: {
        data: []
      },
      searchForm: {
        code: "",
        name: "",
        pageNum: 1,
        pageSize: 10
      },
      detailForm: {},
      dialogVisible: false,
      rules: {
        code: [
          {
            required: true,
            message: "请输入行业编码",
            trigger: "blur"
          }
        ],
        name: [
          {
            required: true,
            message: "请输入行业名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    editIndustry(row) {
      this.detailForm = Object.assign(
        {
          parentCode: row.parentCode,
          parentName: row.name
        },
        row
      );
      this.dialogVisible = true;
    },
    deleteIndustry(row) {
      this.$confirm("此操作将永久删除该行业, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(res => {
        industryDelete(row.id).then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.getList();
        });
      });
    },

    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.detailForm);
          let params = Object.assign({}, this.detailForm);
          delete params.parentName;
          delete params.children;
          delete params.createTime;
          delete params.sort;
          // console.log(params);
          // return;
          if (params.id) {
            industryEdit(params)
              .then(res => {
                this.$message({
                  message: "编辑成功",
                  type: "success"
                });
                this.handleClose();
                this.getList();
              })
              .catch(err => {
                console.log(err);
              });
          } else {
            industryAdd(params)
              .then(res => {
                this.$message({
                  message: "新增成功",
                  type: "success"
                });
                this.handleClose();
                this.getList();
              })
              .catch(err => {
                console.log(err);
              });
          }
          console.log(this.detailForm);
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      setTimeout(() => {
        this.detailForm = {};
        this.$refs.detailForm.resetFields();
      }, 100);
    },
    addIndustry(v = {}) {
      this.detailForm = {
        parentCode: v.code || "0",
        parentName: v.name
      };
      this.dialogVisible = true;
    },
    reset() {
      this.searchForm = {
        code: "",
        name: ""
      };
      this.getList();
    },
    getList() {
      this.listLoading = true;
      industryList(this.searchForm)
        .then(res => {
          res.data.list.map(v => {
            v.children = [];
          });
          this.tableData.data = handleTree(res.data.list, "code", "parentCode");
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    }
  }
};
</script>

<style scoped lang="scss">
.app-container {
}
</style>
